//
//  CCDevice.m
//  CCKit
//
//  Created by tucici on 2019/3/29.
//  Copyright © 2019 tucici. All rights reserved.
//


#import <sys/utsname.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "CCKeyChain.h"
#import "Reachability.h"
#import "CCDevice.h"
@implementation CCDevice
static CCDevice *_ccdevice;
static dispatch_once_t onceToken;
+ (instancetype)device
{
    if (!_ccdevice)
    {
        dispatch_once(&onceToken, ^{
            _ccdevice = [[CCDevice alloc] init];
        });
    }
    return _ccdevice;
}
//CCDeviceStruct *ab(void)
//{
//    CCDeviceStruct *cd = malloc(sizeof(CCDeviceStruct));
//    cd->UUid=_UUids();
//    return cd;
//}
//CCDeviceStruct cd(void)
//{
//    CCDeviceStruct device = {
//        _UUids,
//    };
//    device.UUid=_UUids();
//    return device;
//}
static char * _UUids(void)
{
    char *string = (char *)CFStringGetCStringPtr((CFStringRef)[CCDevice UUID], CFStringGetSystemEncoding());
    return string;
}
+ (NSString *)UUID
{
    // 这个key的前缀最好是你的BundleID
    NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier];
    NSString*uuid = (NSString*)[CCKeyChain load:bundleName];
    //首次执行该方法时，uuid为空
    if([uuid length] < 1)
    {
        // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
        uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        
        if(uuid.length == 0 || [uuid isEqualToString:@"00000000-0000-0000-0000-000000000000"])
        {
            //生成一个uuid的方法
            CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
            uuid = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
            CFRelease(uuidRef);
        }
        //将该uuid保存到keychain
        [CCKeyChain save:bundleName value:uuid];
    }
    return uuid;
}
+ (NSString *)version
{
    return [[UIDevice currentDevice] systemVersion];
}

+ (NSString *)deviceType
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
}
+ (NSString *)networkType
{
    NSString *netconnType = @"";
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus])
    {
        case NotReachable:// 没有网络
        {
            netconnType = @"no network";
        }
            break;
        case ReachableViaWiFi:// Wifi
        {
            netconnType = @"Wifi";
        }
            break;
        case ReachableViaWWAN:// 手机自带网络
        {
            // 获取手机网络类型
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            NSString *currentStatus = info.currentRadioAccessTechnology;
            if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"])
                netconnType = @"GPRS";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"])
                netconnType = @"2.75G EDGE";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"])
                netconnType = @"3G";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"])
                netconnType = @"3.5G HSDPA";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"])
                netconnType = @"3.5G HSUPA";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"])
                netconnType = @"2G";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"])
                netconnType = @"3G";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"])
                netconnType = @"3G";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"])
                netconnType = @"3G";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"])
                netconnType = @"HRPD";
            else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"])
                netconnType = @"4G";
            else
                netconnType = currentStatus;
        }
            break;
        default:
            break;
    }
    return netconnType;
}
+ (BOOL)isIPhoneNotchScreen
{
    BOOL result = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return result;
    }
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            result = YES;
        }
    }
    return result;
}
 
+ (CGFloat)getIPhoneNotchScreenHeight{
    /*
     * iPhone8 Plus  UIEdgeInsets: {20, 0, 0, 0}
     * iPhone8       UIEdgeInsets: {20, 0, 0, 0}
     * iPhone XR     UIEdgeInsets: {44, 0, 34, 0}
     * iPhone XS     UIEdgeInsets: {44, 0, 34, 0}
     * iPhone XS Max UIEdgeInsets: {44, 0, 34, 0}
     */
    CGFloat bottomSpace = 0;
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets safeAreaInsets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;
        switch (UIApplication.sharedApplication.statusBarOrientation) {
            case UIInterfaceOrientationPortrait:
            {
                bottomSpace = safeAreaInsets.bottom;
            }
                break;
            case UIInterfaceOrientationLandscapeLeft:
            {
                bottomSpace = safeAreaInsets.right;
            }
                break;
            case UIInterfaceOrientationLandscapeRight:
            {
                bottomSpace = safeAreaInsets.left;
            }
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
            {
                bottomSpace = safeAreaInsets.top;
            }
                break;
            default:
            {
                bottomSpace = safeAreaInsets.bottom;
            }
                break;
        }
    }
    return bottomSpace;
}
@end
